|
|
|
|
|
|
|
Referencing Forms and Controls from Your Code |
|
|
|
|
|
|
|
|
One other key property of every Visual Basic object is the Name property. The Name property defines a unique identifier by which you can refer to the object in code. Each form, text box, label, and so on must have a unique name. |
|
|
|
|
|
|
|
|
|
All forms in a project must have different names. However, control names have to be unique only for the form on which they are located. That is, you can have a Text1 control on each form in your project, but you can't have two forms called Form1 in your project.  |
|
|
|
|
|
|
|
|
|
|
Visual Basic provides a default name when an object is first created. For example, Form1 is the name given to the first form created for your project, and Text1 is the name given to the first text box that you place on a form. However, the first thing you should usually do after drawing a control or form is to provide it with a name that has some meaning. For example, I often use frmMain as the name of the main interface form in my applications. |
|
|
|
|
|
|
|
|
As mentioned in Chapter 2, following a standard when naming forms and other objects is good programming practice. It's common to use a three-letter contraction of the control's type as a lowercase prefix to identify the type of object to which the name refers; the remainder of the name describes the purpose of the object. In the frmMain form just discussed, the prefix frm indicates that the object is a form, and the suffix Main indicates that it's the main form in the program. Table 3.1 lists suggested prefixes for many of Visual Basic's objects (forms and controls). |
|
|
|
|
| Table 3.1 Visual Basic Object Types and Common Name Prefixes | | Object Type | Name Prefix | Object Type | Name Prefix | | CheckBox | chk | Horizontal ScrollBar | hsb | | ComboBox | cbo | Image | img | | Command Button | cmd | Label | lbl | | Common Dialog | cdl | Line | lin | | Data Control | dat | ListBox | lst | | Data Bound ComboBox | dbc | Menu | mnu | | Data Bound Grid | dbg | OLE Container | ole | | Data Bound ListBox | dbl | Option Button | opt | | Directory ListBox | dir | Picture Box | pic | | Drive ListBox | drv | Shape | shp | | File ListBox | fil | TextBox | txt | | Form | frm | Timer | tmr | | Frame | fra | Vertical ScrollBar | vsb | | Grid | grd |
|
|
|